package com.zhangliang.oa.crud;

import com.zhangliang.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DeptDelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        String contextPath = req.getContextPath();
        String deptno = req.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;
        int i = 0;
        try {
            conn = DBUtil.getConnection();
            String sql ="delete from dept where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,null);
        if (i == 1){
            resp.sendRedirect(contextPath+"/dept/list");
        }else {
            resp.sendRedirect(contextPath+"/error.html");
        }
    }
}
